﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Gift.Model;
using Gift.DAL;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

namespace WebApp.admin.goods
{
    public partial class goodsAgent : BaseAdmin
    {
        tbGoodsAgent oGoodsAgent;
      
        tbGoods oGoods;

        // Methods
        private void GoodsAgentBind()
        {
            try
            {
                oGoodsAgent = null;
                if (Request.QueryString["ot"] == "price")
                {
                    oGoodsAgent = BaseFactory.theGoodsAgent.GetModel(Convert.ToInt32(base.Request.QueryString["gid"]), int.Parse(base.Administrator.CompanyId));
                }
                else if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    oGoodsAgent = BaseFactory.theGoodsAgent.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                }
                if (oGoodsAgent != null)
                {
                    tbGoods oGoods = BaseFactory.theGoods.GetModel(oGoodsAgent.goods);
                    tbGoodsEntitys oGoodsEntitys = BaseFactory.theGoodsEntitys.GetModel(oGoodsAgent.goods);
                    this.lblGoodsName.Text = oGoods.goodsName;
                    this.ddlAgentCompanyList.Items.FindByValue(oGoodsAgent.downCompanyId.ToString());
                    this.txtAgentPrice.Value = oGoodsAgent.agentPrice.ToString();
                    this.lblSalePrice.Text = oGoodsEntitys.salePrice.ToString();
                    this.lblOnSale.Text = (oGoodsAgent.agentPrice / oGoodsEntitys.salePrice).ToString();
                    this.txtAgentMarketPrice.Value = oGoodsAgent.agentMarketPrice.ToString();
                    if (Request.QueryString["ot"] == "price")
                    {
                        this.ddlAgentCompanyList.Items.Clear();
                        tbCompany oCompany = BaseFactory.theCompany.GetModel(oGoodsAgent.companyId);
                        this.ddlAgentCompanyList.Items.Add(new ListItem(oCompany.Name, oCompany.Id.ToString()));
                    }
                }
            }
            catch
            {
            }
        }

        void BindDownCompanyList()
        {
            DataTable dtCompanyList = BaseFactory.theCompany.GetList(" parentId = " + base.Administrator.CompanyId).Tables[0];
            this.ddlAgentCompanyList.DataTextField = "name";
            this.ddlAgentCompanyList.DataValueField = "id";
            this.ddlAgentCompanyList.DataSource = dtCompanyList;
            this.ddlAgentCompanyList.DataBind();
            this.ddlAgentCompanyList.Items.Insert(0, new ListItem("请选择下级商家...", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    oGoodsAgent = BaseFactory.theGoodsAgent.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                }
                else if (Request.QueryString["ot"] != null && Request.QueryString["ot"] == "price")
                {
                    oGoodsAgent = BaseFactory.theGoodsAgent.GetModel(Convert.ToInt32(base.Request.QueryString["gid"]), int.Parse(base.Administrator.CompanyId));

                }
                else
                {
                    oGoodsAgent = new tbGoodsAgent();
                    this.oGoodsAgent.upCompanyId = int.Parse(base.Administrator.CompanyId);
                    this.oGoodsAgent.creater = int.Parse(base.Administrator.AdminUserId);
                    this.oGoodsAgent.downCompanyId = int.Parse(this.ddlAgentCompanyList.SelectedItem.Value);
                }
                tbGoods oGoods = BaseFactory.theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                tbGoodsEntitys oGoodsEntitys = BaseFactory.theGoodsEntitys.GetModel(oGoods.id);
                this.oGoodsAgent.goods = int.Parse(Request.QueryString["gid"]);
                this.oGoodsAgent.companyId = oGoods.CompanyId;
                this.oGoodsAgent.isOrderProcess = this.rblOrderProcess.SelectedItem.Value == "1" ? true : false;
                this.oGoodsAgent.agentPrice = decimal.Parse(this.txtAgentPrice.Value);

                this.oGoodsAgent.agentMarketPrice = this.txtAgentMarketPrice.Value.Trim() == "" ? oGoodsEntitys.marketPrice : decimal.Parse(this.txtAgentMarketPrice.Value);

                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null)
                    || (Request.QueryString["ot"] != null && Request.QueryString["ot"] == "price") )
                {

                    if (BaseFactory.theGoodsAgent.Update(this.oGoodsAgent))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='goodsAgentList.aspx?gid=" + Request.QueryString["gid"] +"';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    this.oGoodsAgent.agentDate = DateTime.Now;
                    if (BaseFactory.theGoodsAgent.Add(this.oGoodsAgent) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");window.location.href='goodsAgentList.aspx?gid=" + Request.QueryString["gid"] + "';</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            base.ValidateLoginRule(Gift.Model.ENUM.ERule.GoodsAgent);
            if (!base.IsPostBack)
            {
                this.BuildForm();
                this.BindDownCompanyList();
            }
            if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
            {
             
                    this.GoodsAgentBind();
            }

        }

        void BuildForm()
        {
            if (base.Request.QueryString["gid"] != null)
            {
                oGoods = BaseFactory.theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                tbGoodsEntitys oGoodsEntitys = BaseFactory.theGoodsEntitys.GetModel(oGoods.id);
                this.lblGoodsName.Text = oGoods.goodsName;
                this.lblMarketPrice.Text = oGoodsEntitys.marketPrice.ToString();
                this.lblSalePrice.Text = oGoodsEntitys.salePrice.ToString();
            }
            if (base.Request.QueryString["ot"] != null)
            {
                  if(base.Request.QueryString["ot"] == "price")
                {
                   
                }
            }
        }

    }
}